(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.dataInquiry.createVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'dataInquiryService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, dataInquiryService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.dataInquiry = {};
		ctrl.dataInquiryOld = {};

		ctrl.serviceCodeOptions = {};
		ctrl.dinqFilebuilderIdOptions = {};
		ctrl.parmTypeOptions = {};
		
		ctrl.init = function() {
			
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.params.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.dataInquiry.paramList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
//			ctrl.sitesLookupOptions = dataInquiryService.getSitesLookupOptions();
//			ctrl.accessTypeLookupOptions = dataInquiryService.getAccessTypeLookupOptions();
//			ctrl.runTypeOptions = dataInquiryService.getRunTypeOptions();
//			ctrl.allowQuoteFlagOptions = dataInquiryService.getAllowQuoteFlagOptions();
			authService.createVerify(params).then(function(response) {
				ctrl.dataInquiry = response.data.model || {};
				ctrl.dataInquiry.taskId = response.data.task.taskId;
				ctrl.dataInquiry.paramList = ctrl.dataInquiry.paramList || [];
				angular.extend(ctrl.serviceCodeOptions,  response.data.model.services);
				angular.extend(ctrl.dinqFilebuilderIdOptions,  response.data.model.dataInquiryBuilders);
				angular.extend(ctrl.parmTypeOptions,  response.data.model.dataTypes);
				
				ctrl.dinqAccessTypeOptions = getDinqAccessTypeOptions().data;
//				ctrl.runTypeOptions = dataInquiryService.getRunTypeOptions();
//				ctrl.allowQuoteFlagOptions = dataInquiryService.getAllowQuoteFlagOptions();
//				ctrl.accessTypeLookupOptions = ctrl.dataInquiry.accessTypeList;
			});
		};
		
		function getDinqAccessTypeOptions() {
			return {
				"data" : [ {
					"label" : "",
					"value" : ",,"
				}]
			};
		}
		
		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.dataInquiry.comments = comments;
						authService.createVerifyReject(ctrl.dataInquiry).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.dataInquiry.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.dataInquiry).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.datainquiry.list',
					'state' : 'cda.dataInquiry.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.datainquiry.list',
					'state' : 'cda.dataInquiry.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);